<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\Mood as mMood;
use think\Db;
use think\Config;

class Mood extends Base
{
    protected $beforeActionList = [
        'beforeEdit' => ['only' => 'edit'],
    ];

    protected function beforeEdit()
    {
        //修改前置操作，判断如果
        $request = Request::instance();
        if ($request->isGet() && $request->has('id')){
            $data = Db::name('mood')->find($request->param('id'));
            $this->assign(['data'=>$data]);
        }else{
           $this->error('请求参数错误','Index/welcome');
        }
    }

    /*管理页面加载
     * */
    public function index()
    {
        return $this->fetch();
    }
    /*列表数据调用
     * */
    public function lists(Request $request)
    {
        if ($request->isPost()) {
            //判断是否有条件查询
            $where = $request->has('where')?$request->param()['where']:[];
            //获取当前页码
            $page = $request->param('page');
            //获取每页显示条数
            $limit = $request->param('limit');
            //过滤空值项
            $where = array_filter($where);

            return mMood::mAll($page,$limit,$where);
        }else{
            return return_json(1,'请求错误！');
        }
    }
    /*增加(页面加载)
     * */
    public function add(){
        return $this->fetch();
    }
    /*
     * 修改(页面加载)
     * */
    public function edit(){
        return $this->fetch();
    }
    /*公共：数据增加或者修改
     *
     * */
    public function save(Request $request){
        //判断是否是post请求过来数据
        if ($request->isPost()) {

            //定义默认为增加操作
            $method = ['create','增加'];
            $data = $request->param()['data'];

            //获取临时的图片地址
            $temp_imgs = getImg($data['digests']);
            $new_imgs = [];
            //替换成实际图片地址
            foreach ($temp_imgs as $v){
                $new_imgs = temp_to_uploads($v);
                $data['digests'] =  str_replace($v,$new_imgs,$data['digests']);
            }


            //判断是否是更新数据
            if ($request->has('data.id')){
                $method = ['update','更新'];
            }
            //验证输入数据是否合法
            $check = $this->validate($data,$request->controller() . '.' .$method[0]);
            if (true !== $check){
                return return_json(1,$check);
            }

            //调用模型增加或者更新静态方法
            return mMood::mSave($data,$method);
        }else{
            return return_json(1,'请求错误！');
        }
    }
    /*
     * 删除
     * */
    public function del(Request $request){
        if ($request->isPost()){
            $data = $request->param()['data'];
            return mMood::mDel($data);
        }else{
            $this->error('请求参数错误','Index/welcome');
        }
    }
    /*状态修改
     * */
    public function status(Request $request){
        if ($request->isPost()){
            $data = $request->param()['data'];
            return mMood::mStatus($data);
        }else{
            $this->error('请求参数错误','Index/welcome');
        }
    }

    /*上传
 * */
    public function upload(Request $request)
    {
        if ($request->isPost()) {
            $temp_upload_path = ROOT_PATH . 'public' . DS . Config::get('temp_upload_path');

            if ($request->isPost()) {

                //判断是否是替换文件，有就删除
                if ($request->has('old')) {

                    $file_path = ROOT_PATH.'public'.DS . $request->param('old');
                    if (file_exists($file_path)) {
                        @unlink($file_path);
                    }

                }

                $data = [
                    'errno' => 0,
                    'data' => [],
                ];

                // 获取表单上传文件
                $files = request()->file('file');
                if ($files) {
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $info = $files->rule('uniqid')->move($temp_upload_path);
                    if ($info) {
                        // 成功上传后 获取上传信息
                        $data['data'][] = '/' . Config::get('temp_upload_path') . '/' . $info->getFilename();

                    } else {
                        // 上传失败获取错误信息
                        $data['msg'] = $files->getError();
                    }
                } else {
                    $data['errno'] = 400;
                }
                return json($data);
            } else {
                return return_json(1, '请求错误！');
            }
        } else {
//            $this->error('请求参数错误','index/welcome');
            return return_json(1, '请求错误！');
        }
    }
}
